// pages/create/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    image: 'https://www.xiaomutong.com.cn/public/subject/Snipaste_2019-07-25_22-10-08.png',
    imagefile: '/public/subject/Snipaste_2019-07-25_22-10-08.png',
    title: '',
    content: '',
    openid: '',
    userInfo: null
  },
  bindCreate: function(){
    let title = this.data.title;
    let content = this.data.content;
  },
  bindKeyInput: function(e) {
    console.log(e.detail.value);
    this.setData({
      title: e.detail.value
    })
  },
  bindTextAreaInput: function(e) {
    console.log(e.detail.value);
    this.setData({
      content: e.detail.value
    })
  },
  bindUploadFile: function(){
    let _this = this;
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths
        console.log(res);
        _this.setData({
          image: tempFilePaths[0]
        })
        wx.uploadFile({
          url: 'https://www.xiaomutong.com.cn/web/index.php?r=site/uploadfile', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'UploadForm[imageFile]',
          formData: {
            'user': 'test'
          },
          success (res){
            const data = JSON.parse(res.data);
            //do something
            console.log(res.data);
            _this.setData({
              imagefile: '/uploads/'+data.result.name
            })
          }
        })
      }
    })
  },
  bindSend: function(){
    let title = this.data.title;
    let content = this.data.content;
    let _this = this;
    wx.request({
      url: 'https://www.xiaomutong.com.cn/web/index.php?r=wechat/checkcontent',
      method: 'post',
      data: {
        content: content
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        //{"code":0,"message":"","result":"{\"errcode\":0,\"errmsg\":\"ok\"}"}
        let result = res.data.result;
        let info = JSON.parse(result);
        console.log(info);
        if(res.data.code == 0 && info.errcode == 0 ){
          _this.create();
        }else{
          //{"code":0,"message":"","result":"{\"errcode\":87014,\"errmsg\":\"risky content hint: [D7iWOA0406b464]\"}"}
          wx.showToast({
            title: '信息敏感',
            icon: 'success',
            duration: 2000
          })
        }
      }
    })
  },
  create: function(){
    let title = this.data.title;
    let content = this.data.content;
    let imagefile = this.data.imagefile;
    let openid = this.data.openid;
    let userInfo = this.data.userInfo;
    wx.request({
      url: 'https://www.xiaomutong.com.cn/web/index.php?r=message/addmessage',
      method: 'post',
      data: {
        openid: openid,
        userinfo: encodeURIComponent(JSON.stringify(userInfo)),
        image: imagefile,
        title: title,
        content: content
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        let result = res.data.result;
        wx.navigateTo({
          url: '/pages/message/index?id='+result.id
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    // 查看是否授权
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo)
              _this.setData({
                userInfo: res.userInfo
              })
            }
          })
        }else{
          let url = '/pages/wechat/index?action=1';
          wx.navigateTo({
            url: url
          })
        }
      }
    })

    wx.login({
      success (res) {
        console.log(res);
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.xiaomutong.com.cn/web/index.php?r=wechat/getinfo3',
            method: 'post',
            data: {
              code: res.code
            },
            success (res) {
              console.log(res.data);
              if(res.data && res.data.code == 0){
                let result = JSON.parse(res.data.result);
                console.log(result);
                _this.setData({
                  openid: result.openid
                })
              }
            },
            fail (err){
              console.log(err);
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    alert('分享进来的');
  }
})